package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @DubboReference
    private UserService userService;

    @RequestMapping("/fail")
    public Result fail(){
        return new Result(false,MessageConst.LOGIN_FAIL);
    }

    @RequestMapping("/success")
    public Result success(){
        log.info("[进入user方法中]");
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    @GetMapping("/findpage")
    public Result findpage(QueryPageBean queryPageBean){
        PageResult findpages = userService.findpage(queryPageBean);
        log.info("[UserController] findPages:{}",findpages);
        if (findpages!=null){
            return new Result(true,MessageConst.QUERY_USER_SUCCESS,findpages);
        }
        return new Result(false,MessageConst.QUERY_USER_FAIL);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user,Integer[] roles){
        log.info("[UserController]user:{},roles:{}",user,roles);
        int add = userService.add(user,roles);
        if (add==1){
            return new Result(true,MessageConst.ADD_USER_SUCCESS);
        }
        return new Result(false,MessageConst.ADD_USER_FAIL);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        User user = userService.findById(id);
        if (user!=null){
            return new Result(true,MessageConst.QUERY_USER_SUCCESS,user);
        }
        return new Result(false,MessageConst.QUERY_USER_FAIL);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roles){
        int i = userService.edit(user,roles);
        if (i==1){
            return new Result(true,MessageConst.EDIT_USER_SUCCESS);
        }
        return new Result(false,MessageConst.EDIT_USER_FAIL);
    }

    @PostMapping("/delete")
    public Result delete(Integer id){
        int i = userService.delete(id);
        if (i==1){
            return new Result(true,MessageConst.DELETE_USER_SUCCESS);
        }
        return new Result(false,MessageConst.DELETE_USER_FAIL);
    }

    @GetMapping("/getUsername")
    public Result getUsername(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal!=null && principal instanceof org.springframework.security.core.userdetails.User){
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)principal;
            return new Result(true,MessageConst.GET_USERNAME_SUCCESS,user.getUsername());
        }
        return new Result(false,MessageConst.GET_USERNAME_FAIL);
    }
}
